package com.jiezuo.flowable.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiezuo.common.base.BaseController;
import com.jiezuo.common.util.ResultUtil;
import com.jiezuo.common.vo.PageVo;
import com.jiezuo.common.vo.Result;
import com.jiezuo.flowable.entity.ModelPageQuery;
import com.jiezuo.flowable.service.UserModelService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.ui.modeler.domain.Model;
import org.flowable.ui.modeler.model.ModelRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description 模型管理 创建/删除/发布
 * @author 庞留杰
 * @date 2020/9/11 18:01
 * @version V1.0
 **/
@Slf4j
@Transactional
@RestController
@RequestMapping("/processModel")
public class UserModelController extends BaseController {

    @Autowired
    UserModelService service;

    /**
     * 功能描述: 新建一个流程模型
     *      参数参考：flowable-ui-modeler-logic-6.5.0.jar->org.flowable.ui.modeler.model.ModelRepresentation
     * @author: 庞留杰
     * @param  modelRepresentation
     *         包含字段：key:模型;Key name:模型名称;description:模型描述信息;modelType:0
     * @exception
     * @date: 2020/9/10 10:25
     */
    @PostMapping(value = "/createModel", produces = "application/json")
    public Result createModel(@RequestBody ModelRepresentation modelRepresentation) {
        ModelRepresentation model = service.createModel(modelRepresentation);
        return new ResultUtil<>().setData(model);
    }

    /**
     * 功能描述: 查询所有流程模型
     * @author: 庞留杰
     * @param pageVo
     * @exception
     * @date: 2020/9/10 10:25
     */
    @GetMapping(value = "/findModelPage", produces = "application/json")
    public Result findModelerPage(@ModelAttribute PageVo pageVo, @ModelAttribute ModelPageQuery modelPageQuery) {
        Page page = service.findModelerPage(pageVo, modelPageQuery);
        return new ResultUtil<>().setData(page);
    }

    /**
     * 功能描述: 查询所有流程模型（不带分页）
     * @author: 庞留杰
     * @param pageVo
     * @exception
     * @date: 2020/9/10 10:25
     */
    @GetMapping(value = "/findModelerList", produces = "application/json")
    public Result findModelerList( @ModelAttribute ModelPageQuery modelPageQuery) {
        List<Model> list = service.findModelerList(modelPageQuery);
        return new ResultUtil<>().setData(list);
    }

    /**
     * 功能描述: 删除流程模型Model
     * @author: 庞留杰
     * @param   id （ModelId）
     * @exception
     * @date: 2020/9/10 10:26
     */
    @PostMapping("deleteModel/{id}")
    public Result deleteModel(@PathVariable("id") String id) {
        service.deleteModel(id);
        return new ResultUtil<>().setSuccessMsg("删除成功");
    }

    /**
     * @description 发布流程图
     * @param  modelId: 流程ID
     * @author 庞留杰
     * @date 2020/9/11 17:59
     * @version V1.0
     **/
    @PostMapping("/deploy/{modelId}")
    public Object deploy(@PathVariable("modelId") String modelId) throws Exception {
        service.deploy(modelId);
        return new ResultUtil<>().setSuccessMsg("操作成功");
    }

}
